package com.system.service;

import com.system.entity.Student;
import com.system.entity.DataException;
import com.system.util.FileUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生服务类
 * 处理学生信息的增删改查
 */
public class StudentService {
    private static final String STUDENT_FILE = "student.txt";

    /**
     * 添加学生
     * @param student 学生对象
     * @throws DataException 数据异常
     */
    public void addStudent(Student student) throws DataException {
        // 验证年龄
        if (student.getAge() < 15 || student.getAge() > 30) {
            throw new DataException("年龄必须在15-30岁之间");
        }

        Student[] students = FileUtil.readFromFile(STUDENT_FILE, Student.class);
        
        // 检查学号是否重复
        for (Student s : students) {
            if (s.getStudentId().equals(student.getStudentId())) {
                throw new DataException("学号已存在");
            }
        }

        // 添加新学生
        List<Student> studentList = new ArrayList<>();
        for (Student s : students) {
            studentList.add(s);
        }
        studentList.add(student);

        FileUtil.writeToFile(STUDENT_FILE, studentList.toArray(new Student[0]));
    }

    /**
     * 删除学生
     * @param studentId 学号
     * @throws DataException 数据异常
     */
    public void deleteStudent(String studentId) throws DataException {
        Student[] students = FileUtil.readFromFile(STUDENT_FILE, Student.class);
        List<Student> studentList = new ArrayList<>();
        boolean found = false;

        for (Student student : students) {
            if (!student.getStudentId().equals(studentId)) {
                studentList.add(student);
            } else {
                found = true;
            }
        }

        if (!found) {
            throw new DataException("该学号不存在");
        }

        FileUtil.writeToFile(STUDENT_FILE, studentList.toArray(new Student[0]));
    }

    /**
     * 修改学生信息
     * @param student 学生对象
     * @throws DataException 数据异常
     */
    public void updateStudent(Student student) throws DataException {
        // 验证年龄
        if (student.getAge() < 15 || student.getAge() > 30) {
            throw new DataException("年龄必须在15-30岁之间");
        }

        Student[] students = FileUtil.readFromFile(STUDENT_FILE, Student.class);
        List<Student> studentList = new ArrayList<>();
        boolean found = false;

        for (Student s : students) {
            if (s.getStudentId().equals(student.getStudentId())) {
                studentList.add(student);
                found = true;
            } else {
                studentList.add(s);
            }
        }

        if (!found) {
            throw new DataException("该学号不存在");
        }

        FileUtil.writeToFile(STUDENT_FILE, studentList.toArray(new Student[0]));
    }

    /**
     * 按学号查询学生
     * @param studentId 学号
     * @return 学生对象
     * @throws DataException 数据异常
     */
    public Student getStudentById(String studentId) throws DataException {
        Student[] students = FileUtil.readFromFile(STUDENT_FILE, Student.class);

        for (Student student : students) {
            if (student.getStudentId().equals(studentId)) {
                return student;
            }
        }

        throw new DataException("该学号不存在");
    }

    /**
     * 按班级查询学生
     * @param className 班级名
     * @return 学生数组
     */
    public Student[] getStudentsByClass(String className) {
        Student[] students = FileUtil.readFromFile(STUDENT_FILE, Student.class);
        List<Student> resultList = new ArrayList<>();

        for (Student student : students) {
            if (student.getClassName().contains(className)) {
                resultList.add(student);
            }
        }

        return resultList.toArray(new Student[0]);
    }

    /**
     * 判断学生是否存在
     * @param studentId 学号
     * @return 是否存在
     */
    public boolean isStudentExists(String studentId) {
        try {
            getStudentById(studentId);
            return true;
        } catch (DataException e) {
            return false;
        }
    }
}